<template>
  <div>
    <div v-for="a in books.records">
      <div class="CateC">
        <div class="CateC_L">
          <el-image class="CateC_L_img" src="https://z3.ax1x.com/2021/04/10/cay7tK.jpg" fit="fill"/>
          <el-button class="CateC_L_but" type="primary" @click="goDetails(a.id)">详细</el-button>
        </div>
        <div class="CateC_R">
          <span class="book_name">{{a.name}}</span>
          <span class="author">{{a.author}}</span>
          <el-rate
                  class="grade"
                  v-model="a.bookScore"
                  disabled
                  show-score
                  text-color="#ff9900"
                  score-template="{value}">
          </el-rate>
          <span class="presentation">{{a.introduce}}</span>
        </div>
      </div>
      <el-divider class="link"></el-divider>
    </div>
    <el-pagination
            class="page"
            v-show="books.total>8"
            background
            @current-change="handleCurrentChange"
            :current-page.sync="current"
            :page-size="books.size"
            layout="prev, pager, next, jumper"
            :total="books.total">
    </el-pagination>
  </div>
</template>

<script>
  import { apiSearch } from "../request/api"

  export default {
    name: "CategoryContent",
    data() {
      return {
        books: "",
        current: 1,
        content : ''
      }
    },
    created () {
      this.$bus.on('Search', x =>{
        this.content = x;
        apiSearch({
          content: this.content,
          index: 1,
          pageSize: 8
        }).then(res => {
          this.books = res.data.books
          this.current = res.data.books.current
          console.log(res)
        })
      })
    },
    methods: {
      // 跳转页数
      handleCurrentChange(val) {
        apiSearch({
          content: this.content,
          index: val,
          pageSize: 8
        }).then(res => {
          this.books = res.data.books;
          console.log(res)
        });
        document.body.scrollTop=document.documentElement.scrollTop=0;
        console.log(`当前页: ${val}`);
      },

      goDetails(bookid) {
        this.$router.push('/Details')
        this.$bus.emit("bookId", bookid);
      }
    }
  }
</script>

<style scoped>
  .CateC {
    display: flex;
    flex-direction: row;
    margin: 50px 0 60px;
  }
  .CateC_L {
    width: 30%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .CateC_L_img {
    width: 236px;
    height: 236px;
  }
  .CateC_L_but {
    width: 120px;
    height: 40px;
    margin-top: 36px;
  }
  .CateC_R {
    width: 65%;
    margin-top: 30px;
    margin-left: 5%;
    display: flex;
    flex-direction: column;
  }
  .book_name {

  }
  .author {
    margin-bottom: 10px;
  }
  .grade {
    margin-bottom: 10px;
  }
  .presentation {
    margin-right: 10%;
    line-height: 1.6;
  }
  .link {

  }
  .page {
    margin: 100px 30% 120px;
  }

</style>
